package com.sunlee.admin.api.module.mall.controller;


import com.sunlee.common.core.entity.PageData;
import com.sunlee.common.core.entity.Result;
import com.sunlee.common.core.utils.AssertUtils;
import com.sunlee.common.core.validator.group.AddGroup;
import com.sunlee.common.core.validator.group.DefaultGroup;
import com.sunlee.common.core.validator.group.UpdateGroup;
import com.sunlee.common.service.dto.CommonUserDTO;
import com.sunlee.common.service.service.CommonUserService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;


/**
 * 用户
 *
 * @author sunlee
 * @since 2022-10-13
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private CommonUserService userService;

    @GetMapping("page")
    @RequiresPermissions("user:page")
    public Result<PageData<CommonUserDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params) {
        PageData<CommonUserDTO> page = userService.pageOwn(params);
        return Result.ok(page);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    @RequiresPermissions("user:info")
    public Result<CommonUserDTO> get(@PathVariable("id") Long id) {
        CommonUserDTO data = userService.getOwn(id);

        return Result.ok(data);
    }

    @PostMapping
    @RequiresPermissions("user:save")
    public Result<Object> save(@Validated({AddGroup.class, DefaultGroup.class}) @RequestBody CommonUserDTO dto) {
        userService.saveOwn(dto);
        return Result.ok();
    }

    @PutMapping
    @RequiresPermissions("user:update")
    public Result<Object> update(@Validated({UpdateGroup.class, DefaultGroup.class}) @RequestBody CommonUserDTO dto) {
        userService.updateOwn(dto);
        return Result.ok();
    }

    @DeleteMapping
    @RequiresPermissions("user:delete")
    public Result<Object> delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        userService.deleteOwn(ids);

        return Result.ok();
    }
}